Conditions | 10 |
Paths | 32 |
Total Lines | 116 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like main.js ➔ drawEvent often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | var eventQueue = []; |
||
243 | function drawEvent(data, svg_area) { |
||
244 | var starting_opacity = 1; |
||
245 | var opacity = 1 / (100 / data.message.length); |
||
246 | if (opacity > 0.5) opacity = 0.5; |
||
247 | |||
248 | var size = data.message.length; |
||
249 | var label_text; |
||
250 | var ring_radius = 80; |
||
251 | var ring_anim_duration = 3000; |
||
252 | svg_text_color = '#FFFFFF'; |
||
253 | |||
254 | switch(data.type){ |
||
255 | case "PushEvent": |
||
256 | label_text = data.actorName + " pushed to " + data.repoName; |
||
257 | edit_color = '#22B65D'; |
||
258 | break; |
||
259 | case "PullRequestEvent": |
||
260 | label_text = data.actorName + " " + |
||
261 | data.action + " " + " a PR for " + data.repoName; |
||
262 | edit_color = '#8F19BB'; |
||
263 | ring_anim_duration = 10000; |
||
264 | ring_radius = 600; |
||
265 | break; |
||
266 | case "IssuesEvent": |
||
267 | label_text = data.actorName + " " + |
||
268 | data.action + " an issue in " + data.repoName; |
||
269 | edit_color = '#ADD913'; |
||
270 | break; |
||
271 | case "IssueCommentEvent": |
||
272 | label_text = data.actorName + " commented in " + data.repoName; |
||
273 | edit_color = '#FF4901'; |
||
274 | break; |
||
275 | case "ForkEvent": |
||
276 | label_text = data.actorName + " forked " + data.repoName; |
||
277 | edit_color = '#0184FF'; |
||
278 | break; |
||
279 | case "CreateEvent": |
||
280 | label_text = data.actorName + " created " + data.repoName; |
||
281 | edit_color = '#00C0C0'; |
||
282 | break; |
||
283 | case "WatchEvent": |
||
284 | label_text = data.actorName + " watched " + data.repoName; |
||
285 | edit_color = '#E60062'; |
||
286 | break; |
||
287 | } |
||
288 | |||
289 | var no_label = false; |
||
290 | var type = data.type; |
||
291 | |||
292 | var abs_size = Math.abs(size); |
||
293 | size = Math.max(Math.sqrt(abs_size) * scale_factor, 3); |
||
294 | |||
295 | Math.seedrandom(data.message); |
||
296 | var x = Math.random() * (width - size) + size; |
||
297 | var y = Math.random() * (height - size) + size; |
||
298 | |||
299 | var circle_group = svg_area.append('g') |
||
300 | .attr('transform', 'translate(' + x + ', ' + y + ')') |
||
301 | .attr('fill', edit_color) |
||
302 | .style('opacity', starting_opacity); |
||
303 | |||
304 | var ring = circle_group.append('circle'); |
||
305 | ring.attr({r: size, stroke: 'none'}); |
||
306 | ring.transition() |
||
307 | .attr('r', size + ring_radius) |
||
308 | .style('opacity', 0) |
||
309 | .ease(Math.sqrt) |
||
310 | .duration(ring_anim_duration) |
||
311 | .remove(); |
||
312 | |||
313 | var circle_container = circle_group.append('a'); |
||
314 | circle_container.attr('xlink:href', data.eventURL); |
||
315 | circle_container.attr('target', '_blank'); |
||
316 | circle_container.attr('fill', svg_text_color); |
||
317 | |||
318 | var circle = circle_container.append('circle'); |
||
319 | circle.classed(type, true); |
||
320 | circle.attr('r', size) |
||
321 | .attr('fill', edit_color) |
||
322 | .transition() |
||
323 | .duration(max_life) |
||
324 | .style('opacity', 0) |
||
325 | .remove(); |
||
326 | |||
327 | circle_container.on('mouseover', function() { |
||
328 | circle_container.append('text') |
||
329 | .text(label_text) |
||
330 | .classed('label', true) |
||
331 | .attr('text-anchor', 'middle') |
||
332 | .attr('font-size', '0.8em') |
||
333 | .transition() |
||
334 | .delay(1000) |
||
335 | .style('opacity', 0) |
||
336 | .duration(2000) |
||
337 | .each(function() { no_label = true; }) |
||
338 | .remove(); |
||
339 | }); |
||
340 | |||
341 | var text = circle_container.append('text') |
||
342 | .text(label_text) |
||
343 | .classed('article-label', true) |
||
344 | .attr('text-anchor', 'middle') |
||
345 | .attr('font-size', '0.8em') |
||
346 | .transition() |
||
347 | .delay(2000) |
||
348 | .style('opacity', 0) |
||
349 | .duration(5000) |
||
350 | .each(function() { no_label = true; }) |
||
351 | .remove(); |
||
352 | |||
353 | // Remove HTML of decayed events |
||
354 | // Keep it less than 50 |
||
355 | if($('#area svg g').length > 50){ |
||
356 | $('#area svg g:lt(10)').remove(); |
||
357 | } |
||
358 | } |
||
359 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.